package com.berchen.controller;

import com.alibaba.excel.EasyExcel;
import com.berchen.common.passdb.model.PassExcelVo;
import com.berchen.common.passdb.model.PassVo;
import com.berchen.common.passdb.service.PassdbService;
import com.berchen.common.result.Result;
import com.berchen.common.result.ResultEnum;
import com.berchen.utils.MD5Util;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("pass/")
public class PassController {

    private static final Logger logger=Logger.getLogger(PassController.class);
    @Autowired
    private PassdbService passdbService;


    /**
     * 添加密码数据到数据库中
     * @param vo
     * @return
     */
    @PutMapping("addPass.cwh")
    @ResponseBody
    public Result addPass(@RequestBody @Valid PassVo vo){

        try {

            boolean boo = passdbService.addPass(vo);
            if(boo) {
                return Result.ok();
            }else {
                return Result.set(ResultEnum.ACCOUNT_IS_EXIST);
            }

        }catch (Exception e){
            logger.error("添加密码错误",e);
            return Result.error();
        }
    }

    /**
     * 查询单个账号 根据账号和应用来查询
     * @param vo
     * @return
     */
    @GetMapping("getPass.cwh")
    @ResponseBody
    public Result getPass(@RequestBody PassVo vo){
        try {
            if(StringUtils.isEmpty(vo.getAccount())&&StringUtils.isEmpty(vo.getApplication())){
                return Result.set(ResultEnum.NO_PARAM);
            }
            PassVo resultVo=passdbService.getPass(vo);

            resultVo.setPassword(MD5Util.convertMD5(resultVo.getPassword().split("a1a")[0]));
            return Result.ok(resultVo);

        }catch (Exception e){
            logger.error("获取密码错误"+e);
            return Result.error();
        }
    }

    @GetMapping("uploadPass.cwh/{application}")
    public void uploadPass(@PathVariable String application, HttpServletResponse response){

        try {
            response.setContentType("application/vnd.ms.ms-excel");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("密码") + ".xlsx");

            // 查询数据库
            PassVo vo = new PassVo();
            vo.setApplication(application);

            List<PassVo> pass = passdbService.getPasss(vo);


            List<PassExcelVo> excels = new ArrayList<>();

            pass.stream().forEach(p->{
                p.setPassword(MD5Util.convertMD5(p.getPassword().split("a1a")[0]));
                PassExcelVo passExcelVo = new PassExcelVo();
                BeanUtils.copyProperties(p,passExcelVo);
                excels.add(passExcelVo);
            });

            EasyExcel.write(response.getOutputStream(), PassExcelVo.class).sheet().doWrite(excels);
        }catch (Exception e){
            logger.error("批量导出错误"+e);
        }
    }
    @GetMapping("uploadPass.cwh")
    public void uploadPass(HttpServletResponse response){
        uploadPass(null,response);
    }

}
